package com.zzmx.springboot.atomikos.config;

import com.zzmx.springboot.atomikos.utils.JdbcBuildPropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.Properties;
/**
 * @Title:DataSourceConfig
 * @Description: TODO(数据源配置)
 * @Author liuxc
 * @Date 2018/4/19
 */
@Configuration
public class DataSourceConfig {

	@Bean(name = "systemDataSource")
	@Primary
	@Autowired
	public DataSource systemDataSource(Environment env) {
		AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
		Properties prop = JdbcBuildPropertiesUtils.build(env, "spring.datasource.druid.systemDB.");
		ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
		ds.setUniqueResourceName("systemDB");
		ds.setPoolSize(5);
		ds.setXaProperties(prop);
		return ds;

	}

	@Autowired
	@Bean(name = "businessDataSource")
	public AtomikosDataSourceBean businessDataSource(Environment env) {
		AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
		Properties prop = JdbcBuildPropertiesUtils.build(env, "spring.datasource.druid.businessDB.");
		ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
		ds.setUniqueResourceName("businessDB");
		ds.setPoolSize(5);
		ds.setXaProperties(prop);

		return ds;
	}
}
